package com.example.qt.ptplatform.controller;

import com.example.qt.ptplatform.service.impl.CheatingDetectionServiceImpl;
import com.example.qt.ptplatform.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/cheating")
public class CheatingAlertController {

    @Autowired
    private CheatingDetectionServiceImpl cheatingDetectionService;

    /**
     * 检查所有作弊行为（只需传递 userId）
     * @param userId 用户ID
     * @return JsonResult 返回所有作弊检查的结果，统一告警
     */
    @GetMapping("/check-all")
    public JsonResult checkAllCheatingBehaviors(@RequestParam Integer userId) {
        StringBuilder alertMessage = new StringBuilder();

        // 检查频繁更换 IP 地址
        String ipCheckResult = cheatingDetectionService.checkFrequentIPChange(userId);
        if (ipCheckResult != null) {
            alertMessage.append(ipCheckResult).append("\n");
        }

        // 检查下载速率
        String downloadRateCheckResult = cheatingDetectionService.checkDownloadRate(userId);
        if (downloadRateCheckResult != null) {
            alertMessage.append(downloadRateCheckResult).append("\n");
        }

        // 检查是否篡改文件
        String fileTamperingCheckResult = cheatingDetectionService.checkFileTampering(userId);
        if (fileTamperingCheckResult != null) {
            alertMessage.append(fileTamperingCheckResult).append("\n");
        }

        // 如果有告警信息，返回
        if (alertMessage.length() > 0) {
//            return JsonResult.error(alertMessage.toString().trim());
            return JsonResult.error("检测到疑似作弊行为");
        } else {
            return JsonResult.success("没有检测到作弊行为");
        }
    }
}
